package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.SaleMapper;
import com.hqyj.dao.TraderAndStorageDao.GoodsMapper;
import com.hqyj.dao.UserMapper;
import com.hqyj.pojo.Goods;
import com.hqyj.pojo.ResultVoH;
import com.hqyj.pojo.Sale;
import com.hqyj.pojo.User;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.service.ISaleService;
import com.hqyj.util.TimeFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lsr
 * @since 2022-10-03
 */
@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, Sale> implements ISaleService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SaleMapper saleMapper;

    @Transactional//添加事务
    @Override
    public ResultVoH buy(Sale sale) {
        ResultVoH resultVoH = new ResultVoH();

        //生成销售记录信息存入数据库
        String userName = sale.getUserName();
        User user = userMapper.selectUserByName(userName);
        sale.setSaleBuyEmail(user.getUserEmail());
        sale.setSaleBuyTel(user.getUserTel());
        sale.setSaleGoodsTime(new Date());
        System.out.println(sale);
        this.save(sale);

        //商品数量减少
        String goodsName = sale.getSaleGoodsName();
        //取出数据
        Goods goods = goodsMapper.selectGoodsByName(goodsName);
        //修改商品数量
        Integer goodsNums = goods.getGoodsNums();//原来的数量
        Integer saleGoodsNums = sale.getSaleGoodsNums();//销售的数量
        goods.setGoodsNums(goodsNums - saleGoodsNums);
        //更新
        goodsMapper.updateNumsGoodsByName(goods);

        resultVoH.setSuccess(true);
        resultVoH.setInfo("购买成功");

        return resultVoH;
    }
    //销售记录
    @Override
    public ResultVoH saleList(Sale sale) {
        ResultVoH resultVoH = new ResultVoH();
        //1.创建分页对象
        Page<Sale> page = new Page<>(sale.getPage(),sale.getRow());
        //创建条件构造器
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();

        wrapper.like(
                sale.getSaleGoodsName()!=null && !sale.getSaleGoodsName().equals(""),
                "sale_goods_name",
                sale.getSaleGoodsName());
        wrapper.like(
                sale.getSaleBuyTel()!=null && !sale.getSaleBuyTel().equals(""),
                "sale_buy_tel",
                sale.getSaleBuyTel());
        //时间根据范围查询
        if (sale.getStartTime() != null && !sale.getStartTime().toString().equals("")){
            //开始时间加一天，前端传来的开始时间都是前一天的00:00:00，所以加一天
            Date date = TimeFormatUtil.addDay(sale.getStartTime(), 1);
            wrapper.ge("sale_goods_time",date);
        }
        if (sale.getEndTime() != null && !sale.getEndTime().toString().equals("")){
            //结束时间加两天，前端传来的时间少一天，还得加上当天的时间，所以加两天
            Date date = TimeFormatUtil.addDay(sale.getEndTime(), 2);
            wrapper.lt("sale_goods_time", date);
        }

        //2.查询分页
        Page<Sale> salePage = saleMapper.selectPage(page, wrapper);
        if (salePage.getRecords().size() == 0){
            resultVoH.setInfo("没有符合条件的查询");
            return resultVoH;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("recordsList", salePage.getRecords());
        map.put("total", salePage.getTotal());
        map.put("totalPage", salePage.getPages());

        resultVoH.setData(map);
        resultVoH.setInfo("查询成功");
        resultVoH.setSuccess(true);
        return resultVoH;
    }

    @Override
    public ResultVoH saleChart() {
        ResultVoH resultVoH = new ResultVoH();

        List<Sale> saleList = saleMapper.selectSaleCharts();
        List<String> goodsList = new ArrayList<>();
        List<Integer> saleNumsList = new ArrayList<>();
        List<Integer> salePriceCountList = new ArrayList<>();

        for (Sale sale : saleList) {
            goodsList.add(sale.getSaleGoodsName());
            saleNumsList.add(sale.getSaleNums());
            salePriceCountList.add(sale.getSalePriceCount());
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsList",goodsList);
        map.put("saleNumsList",saleNumsList);
        map.put("salePriceCountList",salePriceCountList);

        resultVoH.setData(map);
        resultVoH.setSuccess(true);

        return resultVoH;
    }

}
